<?php
/**
 * @ClassName: EquipmentTransferRecords
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-20 15:51
 * @Version: V1.0.0
 */

namespace app\common\model;

use think\Model;

class EquipmentTransferRecords extends Model
{
    protected $name = 'equipment_transfer_records';

    protected $updateTime = false;

    protected $append = ['holder_info', 'use_info'];

    public function getHolderInfoAttr($value, $data)
    {
        switch ($data['operator_type']) {
            case 2:
                // 运营中心
                $res = OperationCenter::get($data['op_related_id']);
                if ($res) {
                    $text = "运营中心：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "运营中心：" . $data['op_related_id'];
                }
                break;
            case 3:
                // 代理
                $res = Agent::get($data['op_related_id']);
                if ($res) {
                    $text = "代理：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "代理：" . $data['op_related_id'];
                }
                break;
            case 4:
                // 门店
                $res = Store::get($data['op_related_id']);
                if ($res) {
                    $text = "门店：" . $res['id'] . "#" . $res['store_name'];
                } else {
                    $text = "门店：" . $data['op_related_id'];
                }
                break;
            default:
                // 平台
                $text = '平台';
                break;
        }

        return $text;
    }

    public function getUseInfoAttr($value, $data)
    {
        switch ($data['receiver_type']) {
            case 2:
                // 运营中心
                $res = OperationCenter::get($data['re_related_id']);
                if ($res) {
                    $text = "运营中心：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "运营中心：" . $data['re_related_id'];
                }
                break;
            case 3:
                // 代理
                $res = Agent::get($data['re_related_id']);
                if ($res) {
                    $text = "代理：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "代理：" . $data['re_related_id'];
                }
                break;
            case 4:
                // 门店
                $res = Store::get($data['re_related_id']);
                if ($res) {
                    $text = "门店：" . $res['id'] . "#" . $res['store_name'];
                } else {
                    $text = "门店：" . $data['re_related_id'];
                }
                break;
            case 5:
                // 客户
                $res = Customer::get($data['re_related_id']);
                if ($res) {
                    $text = "客户：" . $res['id'] . "#" . $res['customer_name'];
                } else {
                    $text = "客户：" . $data['re_related_id'];
                }
                break;
            case 6:
                // 会员
                $res = Member::get($data['re_related_id']);
                if ($res) {
                    $text = "会员：" . $res['id'] . "#" . $res['nickname'];
                } else {
                    $text = "会员：" . $data['re_related_id'];
                }
                break;
            default:
                // 平台
                $text = '平台';
                break;
        }

        return $text;
    }

    /**
     * 说明: 定义反向关联, 关联至设备表
     * @return \think\model\relation\BelongsTo
     */
    public function equipment()
    {
        return $this->belongsTo(Equipment::class, 'device_id', 'id');
    }

}